home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / BlueBox Spy / Blue Box Daemon / source / CNetServerApp.cp next >
Encoding:
Text File  |  1998-08-10  |  5.0 KB  |  167 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CNetServerApp.cp            ©1995-1998 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //    This project is a simple shell to set up server connections.  You can
  5. //  test this program with the NetClient application.
  6.  
  7. #include "CNetServerApp.h"
  8.  
  9. #include <LGrowZone.h>
  10. #include <UDrawingState.h>
  11. #include <UMemoryMgr.h>
  12. #include <UModalDialogs.h>
  13. #include <URegistrar.h>
  14. #include <UDebugging.h>
  15. #include <UEnvironment.h>
  16.  
  17. #include <LCaption.h>
  18. #include <LEditField.h>
  19. #include <LDialogBox.h>
  20. #include <LEditField.h>
  21. #include <LScroller.h>
  22. #include <LStdControl.h>
  23. #include <LTabGroup.h>
  24. #include <LThread.h>
  25. #include <UThread.h>
  26. #include <LCleanupTask.h>
  27.  
  28. #include "CSimpleTCPServer.h"
  29. #include "CSimpleUDPServer.h"
  30. #include "CTerminalPane.h"
  31.  
  32.  
  33. const PP_PowerPlant::ResIDT    ALRT_NoThreadManager = 1000;
  34. const PP_PowerPlant::ResIDT    PPob_ServerSettings = 128;
  35.  
  36.  
  37. // ---------------------------------------------------------------------------
  38. //        • main
  39. // ---------------------------------------------------------------------------
  40.  
  41. int main()
  42. {
  43.  
  44.     SetDebugThrow_(PP_PowerPlant::debugAction_Alert);    // Set Debugging options
  45.     SetDebugSignal_(PP_PowerPlant::debugAction_Alert);
  46.  
  47.     PP_PowerPlant::InitializeHeap(15);                    // Initialize Memory Manager
  48.                                                         // Parameter is number of Master Pointer
  49.                                                         // blocks to allocate
  50.                                         
  51.     PP_PowerPlant::UQDGlobals::InitializeToolbox(&qd);    // Initialize standard Toolbox managers
  52.  
  53.     // Check for Thread Manager.
  54.     if ( !PP_PowerPlant::UEnvironment::HasFeature( PP_PowerPlant::env_HasThreadsManager ) ) {
  55.         ::StopAlert(ALRT_NoThreadManager, nil);
  56.         ::ExitToShell();
  57.     }
  58.  
  59.     new PP_PowerPlant::LGrowZone(20000);                // Install a GrowZone function to catch
  60.                                                         // low memory situations.
  61.  
  62.     // Run the application.
  63.     new PP_PowerPlant::UMainThread;
  64.  
  65.     CNetServerApp theApp;                // create instance of the application
  66.     
  67.     theApp.AddAttachment(new PP_PowerPlant::LYieldAttachment);
  68.  
  69.     theApp.Run();
  70.  
  71.     // Make sure async tasks get cleaned up. This call is VERY IMPORTANT.
  72.     // (Note: LCleanupTask patches ExitToShell, so things get cleaned up
  73.     // appropriately if you kill the application.)
  74.     PP_PowerPlant::LCleanupTask::CleanUpAtExit();
  75.     
  76.     return 0;
  77. }
  78.  
  79.  
  80. // ---------------------------------------------------------------------------
  81. //        • CNetServerApp
  82. // ---------------------------------------------------------------------------
  83. //    Constructor
  84.  
  85. CNetServerApp::CNetServerApp()
  86.     : LDocApplication()
  87. {
  88.     RegisterClass_(PP_PowerPlant::LCaption);            // You must register each kind of
  89.     RegisterClass_(PP_PowerPlant::LDialogBox);            // PowerPlant classes that you use
  90.     RegisterClass_(PP_PowerPlant::LEditField);            // in your PPob resource.
  91.     RegisterClass_(PP_PowerPlant::LScroller);
  92.     RegisterClass_(PP_PowerPlant::LStdButton);            
  93.     RegisterClass_(PP_PowerPlant::LStdCheckBox);
  94.     RegisterClass_(PP_PowerPlant::LTabGroup);
  95.     RegisterClass_(PP_PowerPlant::LWindow);    
  96.     
  97.     RegisterClass_(CTerminalPane);
  98.     
  99.     SetSleepTime(1);                    // increase responsiveness for Networking
  100. }
  101.  
  102.  
  103. // ---------------------------------------------------------------------------
  104. //        • ~CNetServerApp
  105. // ---------------------------------------------------------------------------
  106. //    Destructor
  107.  
  108. CNetServerApp::~CNetServerApp()
  109. {
  110. }
  111.  
  112.  
  113. // ---------------------------------------------------------------------------
  114. //        • MakeNewDocument
  115. // ---------------------------------------------------------------------------
  116. //    What happens to File->New Session from the menu. Creates a settings dialog
  117. //    then waits modally for a configuration. If completed, then opens a new session.
  118.  
  119. PP_PowerPlant::LModelObject*
  120. CNetServerApp::MakeNewDocument()
  121. {
  122.  
  123.     // Show the configuration dialog.
  124.     SInt32 port, count;
  125.  
  126.     PP_PowerPlant::StDialogHandler dialog(PPob_ServerSettings, this);
  127.     Assert_(dialog.GetDialog() != nil);
  128.     dialog.GetDialog()->Show();
  129.     
  130.     while (true) {
  131.         PP_PowerPlant::MessageT hitMessage = dialog.DoDialog();
  132.         if (hitMessage == PP_PowerPlant::msg_Cancel)
  133.             return nil;
  134.         else if (hitMessage == PP_PowerPlant::msg_OK)
  135.             break;
  136.     }
  137.     
  138.     // Get the user values from the dialog fields
  139.     PP_PowerPlant::LEditField* countField = dynamic_cast<PP_PowerPlant::LEditField*>
  140.                                     (dialog.GetDialog()->FindPaneByID('NUML'));
  141.     ThrowIfNil_ (countField);
  142.     count = countField->GetValue();
  143.  
  144.     PP_PowerPlant::LEditField* portField = dynamic_cast<PP_PowerPlant::LEditField*>
  145.                                     (dialog.GetDialog()->FindPaneByID('PORT'));
  146.     ThrowIfNil_ (portField);
  147.     port = portField->GetValue();
  148.     
  149.     PP_PowerPlant::LStdCheckBox* udpOptionBox = dynamic_cast<PP_PowerPlant::LStdCheckBox*>
  150.                                     (dialog.GetDialog()->FindPaneByID('_UDP'));
  151.     ThrowIfNil_ (udpOptionBox);
  152.     bool useUDP = udpOptionBox->GetValue();
  153.     
  154.     if (useUDP) {
  155.         CSimpleUDPServer* theConnection = new CSimpleUDPServer(this);
  156.         theConnection->WaitForUDPData(port);
  157.         return theConnection;
  158.         
  159.     } else {
  160.         CSimpleTCPServer* theConnection = new CSimpleTCPServer(this);
  161.         theConnection->WaitForConnections(count, port);
  162.         return theConnection;
  163.     }
  164. }
  165.  
  166.  
  167.